package com.wangyadong.hobby.javanetwork.uri;

import java.io.*;
import java.net.CacheRequest;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import static com.wangyadong.hobby.javanetwork.input.InputTest.readMultpileAvailable;
import static com.wangyadong.hobby.javanetwork.input.InputTest.readMultpileFixBug;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/7/1015:12
 **/
public class URlTest {

    public static void main(String[] args) throws IOException {
//        testProtocol("httpsd://www/adc.org");
//        testProtocol("https://www/amazon.com/exec/obidos/order2/");
//        testProtocol("ftp://ibiblio.org/pub/languages/javafaq/");
//        testProtocol("mailto:elharo@ibiblio.org");
//        testProtocol("telnet://dibner.poly.edu/");
//        testProtocol("file:///etc/passwd");
//        testProtocol("gopher:gopher://anc.org.za/");
//        testProtocol("ldap://ldap.itd.umich.edu/o=University%20of%20Michigan.c=US?postalAddress");
//        testProtocol("nfs://utopia.poly.edu/usr/tmp");
//        testProtocol("jdbc:mysql://luna.ibiblio.org:3306/NEWS");
//        testProtocol("rmi://ibiblio.org/RenderEngine");
//        testProtocol("jar:http://cafeaulait.org/books/javaio/ioexample/javaio.jar!/com/macfaq/io/StreamCopier.class");
        //定制协议
//        testProtocol("doc:/UsersGuide/release.html");
//        testProtocol("netdoc:/UsersGuide/release.html");
//        testProtocol("systemresource://www.abc.org/+/index.html");
//        testProtocol("verbatim:http://www.abc.org");
//        System.out.println("========================分割线1====================");
//        testUrl();
//        subUrl();
//        URL systemResource = ClassLoader.getSystemResource("application-dev.properties");
//        System.out.println(systemResource.getFile());
//        System.out.println(systemResource.getContent());
//        System.out.println(systemResource.getUserInfo());
//        InputStream inputStream = systemResource.openStream();
//        readMultpileAvailable(inputStream);
//        URLConnection urlConnection = systemResource.openConnection();
//        String contentType = urlConnection.getContentType();
//        System.out.println(contentType + "   " + urlConnection.getContent().getClass().getSimpleName());
        System.out.println("===============================");
//        testURLConnect();
//        testURLConnectAllHeader();

        testFileCollection();
    }

    public static void testFileCollection() throws IOException {
        URL url = new URL("file:///D:/git/spring/spring-study/ioc-container.png");
//        try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
//            while (-1 != reader.read()) {
//                System.out.println( reader.readLine());
//            }
//        }
        String authority = url.getAuthority();
        String file = url.getFile();
        String protocol = url.getProtocol();
        int port = url.getPort();
        System.out.println("authority is "+authority);
        System.out.println("path is "+file);
        System.out.println("protocol is "+protocol);
        System.out.println("port is "+port);
    }

    public static void testURLCollection() throws IOException {
        URL url = new URL("https://blog.csdn.net/qq_21808961/article/details/81561464");
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
            while (-1 != reader.read()) {
                System.out.println( reader.readLine());
            }
        }
        String authority = url.getAuthority();
        String file = url.getFile();
        System.out.println(authority);
        System.out.println(file);
    }


    public static void testURLOpenCollection() throws IOException {
        URL url = new URL("https://w3.caifupad.com/");
        URLConnection urlConnection = url.openConnection();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))) {
            while (-1 != reader.read()) {
                System.out.println( reader.readLine());
            }
        }
    }

    public static void testProtocol(String urlString) {
        URL url = null;
        try {
            //默认80端口
            url = new URL(urlString);
            System.out.println(url.getProtocol() + " is supported");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public static void testURLConnectAllHeader() {
        URL url = testUrl();
        try {
//            CacheRequest request=new CacheRequest() {
//                @Override
//                public OutputStream getBody() throws IOException {
//                    return null;
//                }
//
//                @Override
//                public void abort() {
//
//                }
//            }
            URLConnection urlConnection = url.openConnection();
            for (int j = 1; ; j++) {
                String headerField = urlConnection.getHeaderField(j);
                if (headerField == null) break;
                System.out.println(urlConnection.getHeaderFieldKey(j) + ":" + headerField);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testURLConnect() {
        URL url = testUrl();
        try {
            URLConnection urlConnection = url.openConnection();
            String contentType = urlConnection.getContentType();
            int contentLength = urlConnection.getContentLength();
            String contentEncoding = urlConnection.getContentEncoding();
            Long date = urlConnection.getDate();
            Long expiration = urlConnection.getExpiration();
            Long lastModified = urlConnection.getLastModified();
            Map<String, List<String>> headerFields = urlConnection.getHeaderFields();
            System.out.println("contentType->" + contentType + " contentLength->" + contentLength + " contentEncoding-> "
                    + contentEncoding + "  date-->" + date + "  expiration->" + expiration + " lastModified-->" + lastModified);
            headerFields.forEach((k, v) -> {
                System.out.println("k->" + k + " v=");
                v.stream().forEach(System.out::println);
                System.out.println("=======================");
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static URL testUrl() {
        try {
            //http://www.publiccms.com/2018/05/29/383.html
//            URL url = new URL("http", "www.publiccms.com/2018/05/29/", "383.html");
            URL url = new URL("http://www.publiccms.com/2018/05/29/383.html");
            return url;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void subUrl() {
        URL url = testUrl();
        try {
            URL url1 = new URL(url, "/index.html");
            System.out.println(url1.getHost());
            System.out.println(url1.getPath());
            System.out.println(url1.getUserInfo());
            System.out.println(url1.getFile());
            System.out.println(url1.getAuthority());
            System.out.println(url1.getContent());
            System.out.println(url1.getDefaultPort());
            System.out.println(url1.getQuery());
            System.out.println(url1.getRef());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
